package com.example.shoppingcar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.example.shoppingcar.adapter.StudentAdapter;
import com.example.shoppingcar.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private Spinner spName;
    private ListView lvName;
    private List<String> names;
    //声明ArrayAdapter的对象
    private ArrayAdapter nameAdapter;

    //声明listview
    private ListView lvStudent;
    //声明学生数据源
    private List<Student> students;
    //声明StudentAdapter的对象
    private StudentAdapter studentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取界面控件
        getViews();

        //初始化数据源
        initDate();
        //实例化ArrayAdapter对象
        nameAdapter = new ArrayAdapter(
                this,
                android.R.layout.simple_list_item_1,
                names
        );

        //给listview绑定适配器
        lvName.setAdapter(nameAdapter);
        lvName.deferNotifyDataSetChanged();//当数据的数量有变化时，调用此方法更新


       //实例化StudentAdapter对象
        studentAdapter = new StudentAdapter(
                this,
                R.layout.activity_main_item,
                students
        );

       //给用于显示学生信息的listview绑定适配器
        lvStudent.setAdapter(studentAdapter);

        //注册监听器
        setListener();



    }

    //初始化数据源
    private void initDate() {
        //初始化listview的数据源
        names = new ArrayList<>();
        //往names中添加元素
        names.add("杰克");
        names.add("露丝");
        names.add("李雷");
        names.add("韩梅梅");


        //初始化学生类别数据源
        students = new ArrayList<>();
        //添加学生数据
        Student jack = new Student(
                R.mipmap.xx,
                "杰克",
                "2021012411"
        );
        students.add(jack);
        Student lilei = new Student(
                R.mipmap.xx,
                "李雷",
                "2021012412"
        );
        students.add(lilei);
        Student lisi = new Student(
                R.mipmap.xx,
                "李四",
                "2021012412"
        );
        students.add(lisi);


    }

    //注册监听器
    private void setListener() {
        //给下拉列表框注册Item选择事件监听器
        spName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                //日志显示当前选择的内容
                Log.i("MainActivity",spName.getSelectedItem().toString());


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {


            }
        });


        //给用于显示姓名的listview注册item
        lvName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long id) {
                //用日志显示
                Log.i("MainActivity",names.get(i).toString());
            }
        });

        //给用于显示学生信息的listview注册item点击事件监听器
        lvStudent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long id) {
                //用日志记录当前点击的学生信息
                Log.i("MainActivity",students.get(i).toString());
            }
        });



    }






    //获取界面控件
    private void getViews() {
        //获取下拉列表框
        spName = findViewById(R.id.sp_name);

        //获取用于显示姓名的listview控件
        lvName = findViewById(R.id.lv_name);


        //获取listview用于显示学生信息
        lvStudent = findViewById(R.id.lv_student);

    }




}





